var formatMapList = {0 : "BMP", 1 : "JPEG", 2 : "GIF", 3 : "PSD", 4 : "PNG", 5 : "TIFF", 6 : "TGA", 7 : "EPS", 100 : "AVI", 101 : "MOV", 102 : "RMVB", 103 : "RM", 104 :"FLV", 105 : "MP4", 106 : "3GP", "200" : "HTML"};
var typeMapList = {0 : "图片", 1 : "视频", 2 : "HTML"};

//获得所有站点信息
function getAllLocinfo(){
    var tbody=window.document.getElementById("loc_data");
    $.ajax({
        url:"../../loc/site",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+"" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ j +"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].devId+"</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].name+"</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].height+"cm</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+tdata[i].width+"cm</td>" +
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>"+ convertTemplateToString(tdata[i].templates) + "</td>"+
                        "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" +
                        "<input type='button' value='编辑' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i].id + ")'" +"</input>"+
                        "<input type='button' value='删除' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input>"+
                        "</td>"+
                        "</tr>";
                    tbody.innerHTML =str;
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}


//获得所有站点信息
function getTemplateinfo(){
    var tbody=window.document.getElementById("template_data");
    $.ajax({
        url:"../../template/site",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    str +="<input type='checkbox' name='templates' value='"+ tdata[i].id +"'><tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].id+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+typeMapList[tdata[i].type]+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].name+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].maxSize+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].height+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].width+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ convertFormatToString(tdata[i].format) + "</td>"+
                        "</tr>";
                    tbody.innerHTML =str;
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}


//获得所有站点信息
function getNotResDevinfo(){
    var tbody=window.document.getElementById("dev_data");
    $.ajax({
        url:"../../dev/site/2",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    str +="<input type='checkbox' name='devs' value='" + tdata[i].id+ "' ><tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].id+"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+tdata[i].name+"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].position.province + "-"+ tdata[i].position.city + "-"+ tdata[i].position.district + "</td>"+
                        "</tr>";
                    tbody.innerHTML =str;
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function convertFormatToString(list) {
    var str = "";
    for ( var i = 0; i < list.length; i++){
        str += formatMapList[list[i]];
        str += "; "
    }
    return str;
}




function convertTemplateToString(list) {
    var str = "";
    for ( var i = 0; i < list.length; i++){
        str += list[i];
        str += "号模板; "
    }
    return str;
}

function imageRadioClick() {
    imageVisible();
    videoHiddenAndEmpty();
    h5HiddenAndEmpty();
}

function videoRadioClick() {
    videoVisible();
    imageHiddenAndEmpty();
    h5HiddenAndEmpty();
}

function h5RadioClick() {
    h5Visible();
    imageHiddenAndEmpty();
    videoHiddenAndEmpty();
}


function addButtonClick() {
    location.href="./loc_add.html";
}

function addSubmitButtonClick() {
    var fdata = $("#loc_add_form").serialize();
    $.param(fdata);
    //console.log(fdata);
    $.ajax({
        url:"../../locs/new",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./loc_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

//删除已选资源位
function deleteButtonClick(id){
    var fdata = $("#locs_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../loc/deletion/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./loc_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });

}

// 编辑时，获取id号对应的设备信息，并设初值
function setOriginInfo() {
    var url = window.location.href;
    var site_id = getArgsFromHref(url, "id");
    $.ajax({
        url:"../../template/" + site_id,
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var tdata=data.object;
                document.getElementById("_id").value=tdata.id;
                setType(tdata.type);
                setFormat(tdata.format);
                document.getElementById("_name").value=tdata.name;
                document.getElementById("_height").value=tdata.height;
                document.getElementById("_width").value=tdata.width;
                document.getElementById("_maxSize").value=tdata.maxSize;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function setType(param) {
    if (param == 0) {
        document.getElementById("_type0").click();
    } else if (param == 1) {
        document.getElementById("_type1").click();
    } else {
        document.getElementById("_type2").click();
    }

}

function setFormat(list) {
    for (var i = 0; i < list.length; i++) {
        document.getElementById("_format" + list[i]).click();
    }

}

// 点击编辑按钮时，跳转到编辑页面
function editButtonClick(id){
    // location.href="./template_edit.html?id=" + id;
}

// 编辑时，点击提交按钮
function editSubmitButtonClick(){
    var fdata = $("#template_edit_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../template/edit",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./template_list.html";
            }else if(result.code==1){
                alert(result.message);
                return;
            }
        }
    });
}

// 获取url中的某个参数
function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}



function imageVisible() {
    document.getElementById("_format_label_0").style.display = "inline";
    document.getElementById("_format_label_1").style.display = "inline";
    document.getElementById("_format_label_2").style.display = "inline";
    document.getElementById("_format_label_3").style.display = "inline";
    document.getElementById("_format_label_4").style.display = "inline";
    document.getElementById("_format_label_5").style.display = "inline";
    document.getElementById("_format_label_6").style.display = "inline";
    document.getElementById("_format_label_7").style.display = "inline";
}
function imageHiddenAndEmpty() {
    document.getElementById("_format_label_0").style.display = "none";
    document.getElementById("_format_label_1").style.display = "none";
    document.getElementById("_format_label_2").style.display = "none";
    document.getElementById("_format_label_3").style.display = "none";
    document.getElementById("_format_label_4").style.display = "none";
    document.getElementById("_format_label_5").style.display = "none";
    document.getElementById("_format_label_6").style.display = "none";
    document.getElementById("_format_label_7").style.display = "none";
    document.getElementById("_format0").checked = false;
    document.getElementById("_format1").checked = false;
    document.getElementById("_format2").checked = false;
    document.getElementById("_format3").checked = false;
    document.getElementById("_format4").checked = false;
    document.getElementById("_format5").checked = false;
    document.getElementById("_format6").checked = false;
    document.getElementById("_format7").checked = false;
}

function videoVisible() {
    document.getElementById("_format_label_100").style.display = "inline";
    document.getElementById("_format_label_101").style.display = "inline";
    document.getElementById("_format_label_102").style.display = "inline";
    document.getElementById("_format_label_103").style.display = "inline";
    document.getElementById("_format_label_104").style.display = "inline";
    document.getElementById("_format_label_105").style.display = "inline";
    document.getElementById("_format_label_106").style.display = "inline";
}

function videoHiddenAndEmpty() {
    document.getElementById("_format_label_100").style.display = "none";
    document.getElementById("_format_label_101").style.display = "none";
    document.getElementById("_format_label_102").style.display = "none";
    document.getElementById("_format_label_103").style.display = "none";
    document.getElementById("_format_label_104").style.display = "none";
    document.getElementById("_format_label_105").style.display = "none";
    document.getElementById("_format_label_106").style.display = "none";
    document.getElementById("_format100").checked = false;
    document.getElementById("_format101").checked = false;
    document.getElementById("_format102").checked = false;
    document.getElementById("_format103").checked = false;
    document.getElementById("_format104").checked = false;
    document.getElementById("_format105").checked = false;
    document.getElementById("_format106").checked = false;
}

function h5Visible() {
    document.getElementById("_format_label_200").style.display = "inline";
}

function h5HiddenAndEmpty() {
    document.getElementById("_format_label_200").style.display = "none";
    document.getElementById("_format200").checked = false;
}
